package marksys.free.com.marksys.ui.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import marksys.free.com.marksys.R;
import marksys.free.com.marksys.utils.DensityUtil;
import timber.log.Timber;

/**
 * @author yawei
 * @data on 2018/8/2 下午6:02
 * @email zyawei@live.com
 */
public class SimpleTextAdapterT<KeyType> extends RecyclerView.Adapter<SimpleTextAdapterT.SimpleHolder> {
    private List<KeyType> keys;
    private final HashSet<KeyType> checkedIds = new HashSet<>();
    private final List<TextView> checkBoxes = new ArrayList<>();

    public SimpleTextAdapterT() {

    }

    public SimpleTextAdapterT(boolean checkable) {
        this(checkable, true);
    }

    public SimpleTextAdapterT(boolean checkable, boolean singleCheck) {
        this.singleCheck = singleCheck;
        this.checkable = checkable;
    }

    public SimpleTextAdapterT(boolean checkable, boolean singleCheck, boolean mustHaveCheck) {
        this.singleCheck = singleCheck;
        this.checkable = checkable;
        this.mustHaveCheck = mustHaveCheck;
    }

    public void setDefault(KeyType item) {
        Timber.i("keys %s",keys.toString());
        Timber.i("checkedIds %s",checkedIds.toString());
        checkedIds.clear();
        if (keys.contains(item)) {
            checkedIds.add(item);
        }
        notifyDataSetChanged();
    }

    public void setGrayDefault(int position, Context context) {
        KeyType key = keys.get(position);
        if(key instanceof String){
            for(TextView textView :checkBoxes){
                if(String.valueOf(key).equals(textView.getText().toString())){
                    textView.setTextColor(context.getResources().getColor(R.color.gray));
                }
            }
        }
    }

    public void setDefault(int position) {
        checkedIds.clear();
        checkedIds.add(keys.get(position));
        notifyDataSetChanged();
    }

    public void replace(List<KeyType> items) {
        keys = items;
        if (null != items && items.size() > 0) {
            setDefault(0);
        } else {
            notifyDataSetChanged();
        }
    }

    public SimpleTextAdapterT(List<KeyType> items) {
        keys = items;
    }

    public void setOnClickListener(OnClickListener<KeyType> onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener<KeyType> onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
        checkable = true;
        singleCheck = true;
    }

    public void setOnMultiCheckChangeListener(OnMultiCheckChangeListener<KeyType> onMultiCheckChangeListener) {
        this.onMultiCheckChangeListener = onMultiCheckChangeListener;
        checkable = true;
        singleCheck = false;
    }


    public KeyType getCheckedId() {
        return checkedIds.size() == 0 ? null : new ArrayList<>(checkedIds).get(0);
    }

    public List<KeyType> getCheckedIds() {
        return checkedIds.size() == 0 ? Collections.emptyList() : new ArrayList<>(checkedIds);
    }

    public KeyType getItem(int position) {
        return keys.get(position);
    }


    private boolean singleCheck;
    private boolean mustHaveCheck;
    private boolean checkable;


    @NonNull
    @Override
    public SimpleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        TextView textView = createView(parent);
        checkBoxes.add(textView);
        return new SimpleHolder(textView);
    }

    @Override
    public int getItemCount() {
        return keys == null ? 0 : keys.size();
    }

    @Override
    public void onBindViewHolder(@NonNull SimpleHolder holder, int position) {

        bindView(holder.checkBox, keys.get(position));
        KeyType key = keys.get(position);
        if (holder.checkBox instanceof CompoundButton) {
            Iterator<KeyType> iterator = checkedIds.iterator();
            if (iterator.hasNext()) {
                KeyType next = iterator.next();
                if (next == key) {
                    for (TextView checkBox : checkBoxes) {
                        if (checkBox != holder.checkBox && checkBox instanceof CompoundButton && ((CompoundButton) checkBox).isChecked()) {
                            ((CheckBox) checkBox).setChecked(false);
                        }
                    }
                    ((CompoundButton) holder.checkBox).setChecked(true);
                }
            }
        }
        holder.checkBox.setOnClickListener(v -> {
            if (checkable) {
                Timber.i("keys2 %s",keys.toString());
                Timber.i("checkedIds2 %s",checkedIds.toString());
                boolean contains = checkedIds.contains(key);
                if/*单选*/ (singleCheck) {
                    if (mustHaveCheck) {
                        Iterator<KeyType> iterator = checkedIds.iterator();
                        if (iterator.hasNext()) {
                            KeyType next = iterator.next();
                            if (next == key) {
                                ((CompoundButton) holder.checkBox).setChecked(true);
                                return;
                            }
                        }
                    }

                    if (!contains) {
                        checkedIds.clear();
                        checkedIds.add(key);
                        if (v instanceof CheckBox) {
                            ((CheckBox) v).setChecked(true);
                        }
                        for (TextView checkBox : checkBoxes) {
                            if (checkBox != v && checkBox instanceof CheckBox) {
                                ((CheckBox) checkBox).setChecked(false);
                            }
                        }
                        if (null != onCheckChangeListener) {
                            onCheckChangeListener.onChange(keys.get(position), true);
                        }
                    } else {
                        checkedIds.clear();
                        if (null != onCheckChangeListener) {
                            onCheckChangeListener.onChange(keys.get(position), false);
                        }
                    }

                } /*多选*/ else {
                    if (contains) {
                        checkedIds.remove(key);
                        if (v instanceof CheckBox) {
                            ((CheckBox) v).setChecked(false);

                        }
                    } else {
                        checkedIds.add(key);
                        if (v instanceof CheckBox) {
                            ((CheckBox) v).setChecked(true);
                        }
                    }

                    if (null != onMultiCheckChangeListener) {
                        onMultiCheckChangeListener.onChange(new ArrayList<>(checkedIds));
                    }
                }
            }
            if (null != onClickListener) {
                onClickListener.onClick(key);
            }
        });
    }

    public static class SimpleHolder extends RecyclerView.ViewHolder {
        TextView checkBox;

        SimpleHolder(View itemView) {
            super(itemView);
            checkBox = (TextView) itemView;
        }
    }

    protected TextView createView(ViewGroup parent) {
        CheckBox checkBox = new CheckBox(parent.getContext());
        checkBox.setTextColor(parent.getResources().getColorStateList(R.color.color_filter));
        checkBox.setButtonDrawable(null);
        Drawable drawable = parent.getResources().getDrawable(R.drawable.simple_adapter_checkbox_bg);
        checkBox.setBackgroundDrawable(drawable);
        checkBox.setTextSize(15);
        checkBox.setGravity(Gravity.CENTER);
        checkBox.setPadding(0, DensityUtil.dip2px(parent.getContext(), 4), 0, DensityUtil.dip2px(parent.getContext(), 4));
        checkBox.setButtonDrawable(new ColorDrawable(0));
        return checkBox;
    }

    protected void bindView(TextView textView, KeyType data) {
        textView.setText(data.toString());
    }


    private OnClickListener<KeyType> onClickListener;
    private OnCheckChangeListener<KeyType> onCheckChangeListener;
    private OnMultiCheckChangeListener<KeyType> onMultiCheckChangeListener;

    public interface OnClickListener<KeyType> {
        void onClick(KeyType id);
    }

    public interface OnMultiCheckChangeListener<KeyType> {
        void onChange(List<KeyType> checkedIds);
    }

    public interface OnCheckChangeListener<KeyType> {
        void onChange(KeyType id, boolean checked);
    }
}

